<?php

class guideAction extends baseAction {

    public function index() {
        $gmodel = D('guide');
        $items_cate_mod = D('items_cate');
        $seller_list_mod = D('seller_list');

        $seller_list = $seller_list_mod->get_list();

        $cate_list = $items_cate_mod->get_top_list();

        import("ORG.Util.Page");
        $count = $gmodel->count();
        $p = new Page($count, 20);
        $guide_list = $gmodel->limit($p->firstRow . ',' . $p->listRows)->order('gid ASC')->select();
        $page = $p->show();
        $this->assign('guide_list', $guide_list);
        $this->assign('cate_list', $cate_list);
        $this->assign('seller_list', $seller_list);
        $this->assign('page', $page);
        $this->display();
    }

    function edit() {
        if (isset($_POST['dosubmit'])) {
            $guide_mod = D('guide');
            if (false === $data = $guide_mod->create()) {
                $this->error($guide_mod->error());
            }
            //上传文件
            if ($_FILES['image']['name'] != '') {
                $upload_list = $this->_upload();
                $data['image'] = $upload_list['0']['savename'];
            }
            $data['endtime'] = strtotime($_POST['endtime']);
            $data['createtime'] = time();
            $data['guide_add']['detail'] = $_POST['detail'];
            $guide_info = $guide_mod->where('gid=' . $data['gid'])->find();
            if (!empty($guide_info)) {
                $result = $guide_mod->relation('guide_add')->save($data);
            }
            if ($result) {
                $this->success('修改成功');
            } else {
                $this->error($guide_mod->error());
            }
        } else {
            $guide_mod = D('guide');
            if (isset($_GET['id'])) {
                $guide_id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error(L('please_select'));
            }
            $items_cate_mod = D('items_cate');
            $seller_list_mod = D('seller_list');

            $cate_list = $items_cate_mod->get_top_list();
            $seller_list = $seller_list_mod->where('status=1')->select();
            $guide_info = $guide_mod->where('gid=' . $guide_id)->relation('guide_add')->find();

            $this->assign('seller_list', $seller_list);
            $this->assign('item_cate', $cate_list);
            $this->assign('guide_info', $guide_info);
            $this->display();
        }
    }

    function add() {
        if (isset($_POST['dosubmit'])) {
            $guide_mod = D('guide');
            if (false === $data = $guide_mod->create()) {
                $this->error($guide_mod->error());
            }
            //上传文件
            if ($_FILES['image']['name'] != '') {
                $upload_list = $this->_upload();
                $data['image'] = $upload_list['0']['savename'];
            }
            $data['endtime'] = strtotime($_POST['endtime']);
            $data['createtime'] = time();
            $data['guide_add']['detail'] = $_POST['detail'];
            $result = $guide_mod->relation('guide_add')->add($data);
            if ($result) {
                $this->success('添加成功');
            } else {
//                $this->error('添加失败');
                $this->error($guide_mod->error());
            }
        } else {
            $items_cate_mod = D('items_cate');
            $seller_list_mod = D('seller_list');

            $cate_list = $items_cate_mod->get_top_list();
            $seller_list = $seller_list_mod->where('status=1')->select();
            $this->assign('seller_list', $seller_list);
            $this->assign('item_cate', $cate_list);
            $this->display();
        }
    }
    
    function delete() {
        $guide_mod = D('guide');
        if ((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
            $this->error('请选择要删除记录！');
        }
        if (isset($_POST['id']) && is_array($_POST['id'])) {
            $guide_ids = implode(',', $_POST['id']);
            $guide_mod->relation('guide_add')->delete($guide_ids);
        } else {
            $guide_id = intval($_GET['id']);
            $guide_mod->where('gid=' . $guide_id)->relation('guide_add')->delete();
        }
        $this->success(L('operation_success'));
    }

    public function _upload() {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        $upload->savePath = './data/guide/';

        $upload->saveRule = uniqid;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
        }
        return $uploadList;
    }

    //修改状态
    function status() {
        $article_mod = D('guide');
        $id = intval($_REQUEST['id']);
        $type = trim($_REQUEST['type']);
        $sql = "update " . C('DB_PREFIX') . "guide set $type=($type+1)%2 where gid='$id'";
        $res = $article_mod->execute($sql);
        $values = $article_mod->field("gid," . $type)->where('gid=' . $id)->find();
        $this->ajaxReturn($values[$type]);
    }

}

?>